package com.ice.ucenter.controller;

import com.ice.common.domain.UserJwt;
import com.ice.domain.model.Tree;
import com.ice.domain.model.response.CommonCode;
import com.ice.domain.model.response.QueryResponseResult;
import com.ice.domain.model.response.QueryResult;
import com.ice.domain.ucenter.DeptDO;
import com.ice.domain.ucenter.RoleDO;
import com.ice.domain.ucenter.UserDO;
import com.ice.domain.ucenter.vo.UserVO;
import com.ice.framework.web.BaseController;
import com.ice.ucenter.service.RoleService;
import com.ice.ucenter.service.UserService;
import com.ice.utils.MD5Util;
import com.ice.utils.Query;
import com.ice.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
 * @author Administrator 用户管理中心
 * @version 1.0
 **/
@RequestMapping("/ucenter/user")
@Controller
public class UserController extends BaseController {
	private String prefix="system/user"  ;
	@Autowired
	UserService userService;
	@Autowired
	RoleService roleService;

	@GetMapping("")
	String user(Model model) {
		return prefix + "/user";
	}

	@GetMapping("/list")
	@ResponseBody
	public QueryResponseResult<UserDO> list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<UserDO> sysUserList = userService.list(query);
		int total = userService.count(query);

		QueryResult<UserDO> queryResult = new QueryResult<UserDO>();
		queryResult.setList(sysUserList);
		queryResult.setTotal(total);

		return new QueryResponseResult(CommonCode.SUCCESS,queryResult);
	}

	@GetMapping("/add")
	String add(Model model) {
		List<RoleDO> roles = roleService.list();
		model.addAttribute("roles", roles);
		return prefix + "/add";
	}

	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		UserDO userDO = userService.get(id);
		model.addAttribute("user", userDO);
		List<RoleDO> roles = roleService.list(id);
		model.addAttribute("roles", roles);
		return prefix+"/edit";
	}

	@PostMapping("/save")
	@ResponseBody
	R save(UserDO user) {
		user.setPassword(MD5Util.getStringMD5(user.getPassword()));
		if (userService.save(user) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/update")
	@ResponseBody
	R update(UserDO user) {
		if (userService.update(user) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/updatePeronal")
	@ResponseBody
	R updatePeronal(UserDO user) {
		if (userService.updatePersonal(user) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/remove")
	@ResponseBody
	R remove(Long id) {
		if (userService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") Long[] userIds) {
		int r = userService.batchremove(userIds);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/exit")
	@ResponseBody
	boolean exit(@RequestParam Map<String, Object> params) {
		// 存在，不通过，false
		return !userService.exit(params);
	}

	@GetMapping("/resetPwd/{id}")
	String resetPwd(@PathVariable("id") Long userId, Model model) {

		UserDO userDO = new UserDO();
		userDO.setUserId(userId);
		model.addAttribute("user", userDO);
		return prefix + "/reset_pwd";
	}

	@PostMapping("/resetPwd")
	@ResponseBody
	R resetPwd(UserVO userVO) {
		UserDO userDO = new UserDO();
		UserJwt userJwt = getUser();
		//userDO.setUserId(userJwt.getId());
		//userDO.setUsername(userJwt.getName());
		//userDO.setPassword();
		try{

			userService.resetPwd(userVO,userDO);
			return R.ok();
		}catch (Exception e){
			return R.error(1,e.getMessage());
		}

	}

	@PostMapping("/adminResetPwd")
	@ResponseBody
	R adminResetPwd(UserVO userVO) {
		try{
			userService.adminResetPwd(userVO);
			return R.ok();
		}catch (Exception e){
			return R.error(1,e.getMessage());
		}

	}
	@GetMapping("/tree")
	@ResponseBody
	public Tree<DeptDO> tree() {
		Tree<DeptDO> tree = new Tree<DeptDO>();
		tree = userService.getTree();
		return tree;
	}

	@GetMapping("/treeView")
	String treeView() {
		return  prefix + "/userTree";
	}

	@GetMapping("/personal")
	String personal(Model model) {
		UserDO userDO  = userService.get(Long.parseLong(getUserId()));
		model.addAttribute("user",userDO);
		return prefix + "/personal";
	}
	@ResponseBody
	@PostMapping("/uploadImg")
	R uploadImg(@RequestParam("avatar_file") MultipartFile file, String avatar_data, HttpServletRequest request) {
		return R.error("此功能暂未开启！");
	}
}
